package com.ytkj.digitalConstruction.controller.message;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.ytkj.digitalConstruction.ReturnCode;
import com.ytkj.digitalConstruction.entity.message.Message;
import com.ytkj.digitalConstruction.entity.user.User;
import com.ytkj.digitalConstruction.httpbean.PageParam;
import com.ytkj.digitalConstruction.httpbean.PageResult;
import com.ytkj.digitalConstruction.httpbean.message.reqbean.AddMessageParam;
import com.ytkj.digitalConstruction.httpbean.message.resbean.MessageDetails;
import com.ytkj.digitalConstruction.httpbean.message.resbean.ResMessage;
import com.ytkj.digitalConstruction.service.message.MessageService;
import com.ytkj.digitalConstruction.service.user.UserService;
import com.ytkj.digitalConstruction.util.WebTokenUtil;

/**
 * 消息的控制类
 * 
 * @author momei
 *
 */
@RestController
@RequestMapping("message")
public class MessageController {
	
	Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	MessageService messageService;
	@Autowired
	UserService userService;

	/**
	 * 发送消息
	 * 
	 * @param param 添加的消息所需的实体对象
	 * @param result
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("/add")
	public ReturnCode insert(@RequestBody @Valid AddMessageParam param,BindingResult result,HttpServletRequest request){
		
		// 判断参数是否合法
		if (result.hasErrors()) {
			String msg = result.getFieldError().getDefaultMessage();
			return new ReturnCode.Builder().Success(false).Msg(msg).build();
		}	
		
		//对参数进行处理
		Message message = param.message();
		
		String webToken = request.getHeader("x-access-token");
		User currentUser = userService.getUserByWebToken(webToken);
		if(currentUser == null){			
			return new ReturnCode.Builder().Success(false).Msg("获取当心用户信息失败!").build();
		}

		// 插入消息
		Integer reCode = messageService.insert(message,param.getUserIds(),currentUser);
		if (reCode == null || reCode == 0) {
			log.error("MessageController insert exception.");
			return new ReturnCode.Builder().Success(false).Msg("添加失败!").build();
		}
		return new ReturnCode.Builder().Success(true).Msg("添加成功!").Object(message).build();

	}
	/**
	 * 
	 * @Title: deleteMessage
	 * @Description: 删除消息
	 * @return ReturnCode
	 * @throws
	 */
	@GetMapping("/deleteMessage/{id}")
	public ReturnCode deleteMessage(@PathVariable Integer id) {
		//判断id是否为空
		if(id==null || id < 1){
			log.error("传入的消息id" + id + "不合法");
			return new ReturnCode.Builder().Success(false).Msg("传入id为空").build();
		}
		
		//删除消息
		//判断消息删除是否成功
		Integer  tresult = messageService.deleteMessage(id);
		if(tresult == null || tresult==0) {
			log.error("消息删除失败");
			return new ReturnCode.Builder().Success(false).Msg("消息删除失败").build();
		}
		log.error("消息删除成功");
		return  new ReturnCode.Builder().Success(true).Msg("消息删除成功").build();
	}
	/**
	 * 
	 * @Title: paginationSelect
	 * @Description: 分页查询
	 * @param pageParam
	 * @param result
	 * @return pageResult
	 * @throws
	 */
	@PostMapping("/list")
	public ReturnCode list(@RequestBody @Valid PageParam pageParam,BindingResult result,HttpServletRequest request) {
		// 判断参数是否合法
		if (result.hasErrors()) {
			String msg = result.getFieldError().getDefaultMessage();
			log.error(msg);
			return new ReturnCode.Builder().Success(false).Msg(msg).build();
		}
		String webToken = request.getHeader("x-access-token");
		Integer userId = (Integer)WebTokenUtil.parseWebToken(webToken,"userId");
		Page<ResMessage> resMessageList = messageService.select(pageParam,userId);
		PageResult<ResMessage> pageResult = new PageResult<ResMessage>();
		// 查询所有未删除消息
		log.info("查询所有消息条数开始");
		pageResult.setItemTotal(resMessageList.getTotal());
		log.info("查询所有消息条数结束");
		pageResult.setItems(resMessageList.getResult());
		log.info("查询所有消息成功");
		return new ReturnCode.Builder().Success(true).Msg("查询成功!").Object(pageResult).build();
	}

	/**
	 * 根据消息的id查询对应用户的已读未读情况
	 * 
	 * @param id 消息的id
	 * @return tetailsList
	 */
	@GetMapping("/details/{id}")
	public ReturnCode details(@PathVariable Integer id){
		if(id < 1){
			return new ReturnCode.Builder().Success(false).Msg("参数有误！").build();
		}
		List<MessageDetails> detailsList = messageService.selectDetails(id);
		if(detailsList.isEmpty()){
			return new ReturnCode.Builder().Success(true).Msg("查询结果为空！").build();
		}
		return new ReturnCode.Builder().Success(true).Msg("查询成功！").Object(detailsList).build();
	}
	
}
